package gui.templates;

import gui.EntryPresentation;
import gui.GuiUtils;
import gui.RowPresentation;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import logic.Entry;
import logic.Row;
import logic.TemplateEntry;

public class TemplateRowPresentation extends RowPresentation {

	public class TemplateEntryPresentation implements EntryPresentation {

		@Override
		public JComponent visulize(final Entry e) {
			final TemplateEntry te = (TemplateEntry) e;
			final JPanel panel = new JPanel();
			panel.setName("entry pannel");
			panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

			final JTextField labelField = new JTextField(e.getLabel());
			/*
			 * labelField.addActionListener(new ActionListener(){
			 * 
			 * @Override public void actionPerformed(ActionEvent e) {
			 * te.setLabel(labelField.getText()); } });
			 */
			labelField.getDocument().addDocumentListener(
					new DocumentListener() {

						@Override
						public void changedUpdate(DocumentEvent e) {
							te.setLabel(labelField.getText());

						}

						@Override
						public void insertUpdate(DocumentEvent e) {
							te.setLabel(labelField.getText());

						}

						@Override
						public void removeUpdate(DocumentEvent e) {
							te.setLabel(labelField.getText());

						}
					});
			JLabel label = new JLabel("label:");
			JPanel topPanel = GuiUtils.borderPanel(panel);

			topPanel.add(label, BorderLayout.WEST);
			topPanel.add(GuiUtils.iconButton(GuiUtils.DELETE_ICON,
					"", new ActionListener() {

						public void actionPerformed(ActionEvent event) {
							e.removeFromParent();
							Container parent = panel.getParent();
							parent.remove(panel);
							parent.getParent().validate();
						}

					}), BorderLayout.EAST);
			panel.add(labelField);

			return panel;
		}

	}

	public TemplateRowPresentation() {
		ep = new TemplateEntryPresentation();
	}

	@Override
	public void preVisulize(final Row row, final JPanel panel) {
		JButton deleteButton = GuiUtils.iconButton(GuiUtils.DELETE_ICON,
				"", new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						row.removeFromParent();
						Container parent = panel.getParent();
						parent.remove(panel);
						parent.getParent().validate();
					}
				});
		panel.add(deleteButton);
		// panel.add(new JPanel());
	}

	@Override
	public void postVisulize(final Row row, final JPanel panel) {
		final JPanel addPanel = new JPanel();
		addPanel.setName("add button panel");
		// addPanel.setLayout(new BorderLayout());

		JButton addButton = GuiUtils.iconButton(GuiUtils.ADD_ICON,
				"", new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						Entry entry = row.createEntry();
						panel.add(ep.visulize(entry),
								panel.getComponentCount() - 2);
						panel.getParent().getParent().validate();
					}
				});

		panel.add(addPanel);
		panel.add(addButton/* ,BorderLayout.EAST */);

	}

}
